package com.erikpasternak.vader;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

public class VaderActivity extends Activity implements AdkHelper.Listener {
	public static final String TAG = "Vader";

	/*
	 * TODO:
	 * 2. way to filter for complete drinks only
	 * 3. Stronger colors on drinks list
	 */

	public static final int STATE_INITIAL = 0;
	public static final int STATE_INTRO = 1;
	public static final int STATE_VIDEO = 2;
	public static final int STATE_DRINK = 3;
    public static final int STATE_SETTINGS = 4;
    public static final int STATE_POUR_READY = 5;
    public static final int STATE_WAITING = 6;

	private static final String KEY_STATE = "state";

    private final DrinkDbHelper mDb = new DrinkDbHelper(this);
	private Button mStartButton;
	private FragmentManager mFm;
	private int mState = STATE_INITIAL;
    private AdkHelper mAdkHelper;
    private SharedPrefsHelper mSharedPrefsHelper;
    private DrinkPourHelper mDrinkPourHelper;

	private boolean mResumed = false;
	private boolean mUpdateState = false;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        if (savedInstanceState != null && savedInstanceState.containsKey(KEY_STATE)) {
        	mState = savedInstanceState.getInt(KEY_STATE);
        }
        mSharedPrefsHelper = new SharedPrefsHelper(this);
        mAdkHelper = new AdkHelper(this);
        mAdkHelper.addListener(this);
        mDrinkPourHelper = new DrinkPourHelper(this);
        mFm = this.getFragmentManager();
        Fragment current = mFm.findFragmentById(R.id.holder);
        mUpdateState = true;
    }

	@Override
	public void onResume() {
		super.onResume();
		mResumed = true;
        mAdkHelper.onResume();
		if (mUpdateState) {
			updateState();
			mUpdateState = false;
		}
	}

	@Override
    public void onPause() {
		super.onPause();
        mAdkHelper.onPause();
		mResumed = false;
	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		super.onSaveInstanceState(savedInstanceState);
		savedInstanceState.putInt(KEY_STATE, mState);
	}

    @Override
    public void onNewIntent(Intent intent) {
        mAdkHelper.onNewIntent(intent);
    }

    public AdkHelper getAdkHelper() {
        return mAdkHelper;
    }

	public VaderStateListener getListener() {
		return mListener;
	}

    public DrinkDbHelper getDbHelper() {
        return mDb;
    }

    public DrinkPourHelper getDrinkPourHelper() {
        return mDrinkPourHelper;
    }

	public interface VaderStateListener {
		public void onStateChange(int change);
	}

	private final VaderStateListener mListener = new VaderStateListener() {
		public void onStateChange(int change) {
			if (change == mState) {
				return;
			}
            if (change == STATE_VIDEO && !mSharedPrefsHelper.getRequireVideo()) {
                change = STATE_DRINK;
			}
			mState = change;
			if (mResumed) {
				updateState();
			} else {
				mUpdateState = true;
			}
		}
	};

    private void updateState() {
        Fragment newFrag = null;
        Log.d(TAG, "Switching to state " + mState);
        switch (mState) {
            case STATE_INITIAL:
                newFrag = new StartFragment();
                break;
            case STATE_INTRO:
                newFrag = new IntroFragment();
                break;
            case STATE_VIDEO:
                newFrag = new VideoFragment();
                break;
            case STATE_DRINK:
                newFrag = new DrinkPickerFragment();
                break;
            case STATE_SETTINGS:
                newFrag = new SettingsFragment();
                break;
            case STATE_POUR_READY:
                newFrag = new PouringFragment();
                break;
            case STATE_WAITING:
                newFrag = new EmergencyFragment();
                break;
            default:
                Log.w(TAG, "Unknown state! Restarting!");
                newFrag = new StartFragment();
                mState = STATE_INITIAL;
                break;
        }
        if (newFrag != null) {
        	mFm.beginTransaction().replace(R.id.holder, newFrag).commit();
        }
	}

    public void onMessageReceived(int command, int length, byte[] data) {
        if (command == AdkHelper.ADK_COMMAND_BUTTON && mState == STATE_INITIAL) {
            mListener.onStateChange(STATE_INTRO);
        }
    }

    public void onSendFailed(int command) {
        Log.d(TAG, "send failed " + command + " in state " + mState);
    }

    @Override
    public void onBackPressed() {
        if (mState != STATE_WAITING) {
            mListener.onStateChange(STATE_INITIAL);
        }
    }
}